import type { IComponent, IWidgetMeta } from '@/types/designer'

/**
 * 生成唯一ID
 */
export const generateId = (prefix = 'comp'): string => {
  return `${prefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}

/**
 * 深度克隆组件
 */
export const cloneComponent = (component: IComponent): IComponent => {
  return JSON.parse(JSON.stringify(component))
}

/**
 * 根据组件类型创建新组件
 */
export const createComponentFromWidget = (widget: IWidgetMeta): IComponent => {
  const now = new Date().toISOString()
  return {
    id: generateId(widget.type),
    type: widget.type,
    name: widget.name,
    props: { ...widget.defaultProps },
    style: { ...widget.defaultStyle },
    children: [],
    events: {},
    data: {},
    createdAt: now,
    updatedAt: now
  }
}

/**
 * 查找组件（递归）
 */
export const findComponent = (components: IComponent[], id: string): IComponent | null => {
  for (const component of components) {
    if (component.id === id) return component
    if (component.children) {
      const found = findComponent(component.children, id)
      if (found) return found
    }
  }
  return null
}

/**
 * 查找组件的父组件
 */
export const findParentComponent = (components: IComponent[], targetId: string): IComponent | null => {
  for (const component of components) {
    if (component.children) {
      const hasTarget = component.children.some(child => child.id === targetId)
      if (hasTarget) return component
      
      const found = findParentComponent(component.children, targetId)
      if (found) return found
    }
  }
  return null
}

/**
 * 获取组件的所有子组件（扁平化）
 */
export const getAllChildComponents = (component: IComponent): IComponent[] => {
  const children: IComponent[] = []
  
  const traverse = (comp: IComponent) => {
    if (comp.children) {
      comp.children.forEach(child => {
        children.push(child)
        traverse(child)
      })
    }
  }
  
  traverse(component)
  return children
}

/**
 * 验证组件配置
 */
export const validateComponent = (component: IComponent): boolean => {
  if (!component.id || !component.type || !component.name) {
    return false
  }
  
  if (component.children) {
    return component.children.every(child => validateComponent(child))
  }
  
  return true
}

/**
 * 获取组件的完整路径
 */
export const getComponentPath = (components: IComponent[], targetId: string): string[] => {
  const path: string[] = []
  
  const findPath = (comps: IComponent[], id: string): boolean => {
    for (const comp of comps) {
      if (comp.id === id) {
        path.unshift(comp.name)
        return true
      }
      
      if (comp.children) {
        if (findPath(comp.children, id)) {
          path.unshift(comp.name)
          return true
        }
      }
    }
    return false
  }
  
  findPath(components, targetId)
  return path
}

/**
 * 计算组件的层级深度
 */
export const getComponentDepth = (components: IComponent[], targetId: string): number => {
  const findDepth = (comps: IComponent[], id: string, depth = 0): number => {
    for (const comp of comps) {
      if (comp.id === id) {
        return depth
      }
      
      if (comp.children) {
        const foundDepth = findDepth(comp.children, id, depth + 1)
        if (foundDepth !== -1) {
          return foundDepth
        }
      }
    }
    return -1
  }
  
  return findDepth(components, targetId)
} 